package cn.daniellee.plugin.lm.menu;

import cn.daniellee.plugin.lm.LotteryMaster;
import cn.daniellee.plugin.lm.component.ItemGenerator;
import cn.daniellee.plugin.lm.core.LotteryCore;
import cn.daniellee.plugin.lm.model.PrizePool;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 奖池概览菜单
 */
public class OverviewMenu {

	public static Inventory generate(Player player) {
		Inventory menu = Bukkit.createInventory(new OverviewMenuHolder(), 45, LotteryMaster.getInstance().getConfig().getString("menu.overview.title", "&6&lPrize pools").replace("&", "§"));

		// 填充角
		int[] cornerPosition = new int[]{ 0, 4, 8, 36, 40, 44 };
		for (int i : cornerPosition) menu.setItem(i, Common.getCorner());
		// 填充边
		int[] borderPosition = new int[]{ 1, 3, 5, 7, 9, 17, 27, 35, 37, 39, 41, 43 };
		for (int i : borderPosition) menu.setItem(i, Common.getBorder());

		String trueStr = Common.getTrueStr();
		String falseStr = Common.getFalseStr();
		String openStr = Common.getOpenStr();
		String closeStr = Common.getCloseStr();

		boolean fullAccess = player.hasPermission("lottery.access.edit");
		List<PrizePool> prizePools = LotteryCore.getPrizePools(fullAccess);

		String name = LotteryMaster.getInstance().getConfig().getString("menu.overview.button.name", "&bName: &d{name} &7({id})");

		List<String> loreDefault = LotteryMaster.getInstance().getConfig().getStringList("menu.overview.button.lore.default");
		List<String> lorePurchace = LotteryMaster.getInstance().getConfig().getStringList("menu.overview.button.lore.purchase");
		List<String> loreOperation = LotteryMaster.getInstance().getConfig().getStringList("menu.overview.button.lore.operation");
		List<String> loreEditable = LotteryMaster.getInstance().getConfig().getStringList("menu.overview.button.lore.editable");
		String rightClick = LotteryMaster.getInstance().getConfig().getString("menu.overview.button.lore.rightClick", "&1| &3Right click to Purchase");

		int[] positions;
		switch (prizePools.size()) {
			case 1:
				positions = new int[]{ 22 };
				break;
			case 2:
				positions = new int[]{ 20, 24 };
				break;
			case 3:
				positions = new int[]{ 20, 22, 24 };
				break;
			case 4:
				positions = new int[]{ 11, 15, 29, 33 };
				break;
			case 5:
				positions = new int[]{ 11, 13, 15, 29, 33 };
				break;
			default:
				positions = new int[]{ 11, 13, 15, 29, 31, 33 };
				break;
		}

		for (int i = 0; i < prizePools.size() && i < positions.length; i++) {
			PrizePool prizePool = prizePools.get(i);
			List<String> lore = new ArrayList<>(loreDefault);
			if (prizePool.isPurchase()) lore.addAll(lorePurchace);
			lore.addAll(loreOperation);
			if (fullAccess) lore.addAll(loreEditable);
			for (int j = 0; j < lore.size(); j++) {
				String item = lore.get(j);
				item = item.replace("{unlimited}", prizePool.isUnlimited() ? trueStr : falseStr)
						.replace("{times}", Integer.toString(prizePool.getRunTimes()))
						.replace("{purchase}", prizePool.isPurchase() ? trueStr : falseStr)
						.replace("{priceType}", LotteryMaster.getInstance().getConfig().getString("menu.common.priceType." + prizePool.getPriceType().toLowerCase(), prizePool.getPriceType()))
						.replace("{price}", Double.toString(prizePool.getPrice()))
						.replace("{toggle}", prizePool.isOpen() ? closeStr : openStr)
						.replace("{rightClick}", prizePool.isPurchase() ? rightClick : "");
				lore.set(j, item);
			}
			menu.setItem(positions[i], ItemGenerator.getItem(name.replace("{name}", prizePool.getName()).replace("{id}", prizePool.getId()), lore, prizePool.getIcon(), prizePool.getIconData()));
		}
		return menu;
	}

	public static class OverviewMenuHolder implements InventoryHolder {
		@Override
		public Inventory getInventory() {
			return Bukkit.createInventory(null, 45);
		}
	}
}
